package com.yonyou.pmclouds.projecttype.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.IllegalRequestArgException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.projecttype.entity.ProjectTypeVO;
import com.yonyou.pmclouds.projecttype.rmiitf.ProjectTypeOutterMaintain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/outter/projecttype")
public class ProjectTypeOutterController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProjectTypeOutterMaintain maintain;

    @ResponseBody
    @RequestMapping("/insertFromOutter")
    public ProjectTypeVO insertFromOutter(@RequestBody @Validated(ProjectTypeVO.OutterValid.class) ProjectTypeVO projectTypeVO,
                                          BindingResult errors,HttpServletRequest request) throws BusinessException {
        //添加租户
        setTenantInfo(projectTypeVO,request);

        if (errors.hasErrors()) {
            throw new IllegalRequestArgException();
        }

        return maintain.insertFromOutter(projectTypeVO);
    }

    @ResponseBody
    @RequestMapping("/updateFromOutter")
    public ProjectTypeVO updateFromOutter(@RequestBody @Validated(ProjectTypeVO.OutterValid.class) ProjectTypeVO projectTypeVO,
                                   BindingResult errors,HttpServletRequest request) throws BusinessException {

        setTenantInfo(projectTypeVO,request);

        if (errors.hasErrors()) {
            throw new IllegalRequestArgException();
        }

        return maintain.updateFromOutter(projectTypeVO);
    }

    @ResponseBody
    @RequestMapping("/deleteFromOutter")
    public String delteFromOutter(@RequestBody @Validated(ProjectTypeVO.OutterValid.class) ProjectTypeVO projectTypeVO,
                                   BindingResult errors,HttpServletRequest request) throws BusinessException {

        setTenantInfo(projectTypeVO,request);

        if (errors.hasErrors()) {
            throw new IllegalRequestArgException();
        }

        return maintain.deleteFromOutter(projectTypeVO);
    }

    private void setTenantInfo(ProjectTypeVO projectTypeVO,HttpServletRequest request) throws BusinessException{
        String tenantId = request.getHeader("accountId");
        if(StringUtils.isEmpty(tenantId))
            throw new BusinessException("未找到对应租户信息");
        RuntimeEnvironment.setTenantId(tenantId);
        projectTypeVO.setPkTenant(tenantId);

    }

    //启动
    @RequestMapping("/enable")
    public Object enable(@RequestBody ProjectTypeVO projectTypeVO) throws BusinessException {
        if (projectTypeVO==null){
            throw new BusinessException("传参错误");
        }
        ProjectTypeVO enable = maintain.enable(projectTypeVO);
        return enable;
    }
    //停止
    @RequestMapping("/disEnable")
    public Object disEnable(@RequestBody ProjectTypeVO projectTypeVO) throws BusinessException {
        if (projectTypeVO==null){
            throw new BusinessException("传参错误");
        }
        ProjectTypeVO[] disEnable = maintain.disEnable(projectTypeVO);
        return disEnable;
    }

}
